<?php

class kernel
{
    static $base_url = null;
    // 是否安装
    static private $__online = null;
    // 是否设置默认时区
    static private $__set_date_default_timezone = null;
    // 异常处理实例
    static private $__exception_instance = null;
    // 类单列容器数组
    static private $__singleton_instance = array();
    // host 映射
    static private $__host_mirrors = null;

    static private $__base_url = [];

    static private $__host_mirrors_count = null;
    // 语言
    static private $__language = null;

    /**
     * 设置时区
     *
     * @return void
     * @Author 王衍生 50634235@qq.com
     */
    static public function date_default_timezone_set()
    {
        if (is_null(self::$__set_date_default_timezone)) {
            $timezone = config::get('app.timezone', 8);
            $timezone = ($timezone >= 0) ? ($timezone * -1) : ('+' . ($timezone * -1));
            date_default_timezone_set('Etc/GMT' . $timezone);
            self::$__set_date_default_timezone = true;
        }
    }

    /**
     * 设置错误异常处理
     *
     * @return void
     * @Author 王衍生 50634235@qq.com
     */
    static public function startExceptionHandling()
    {
        self::exceptionBootstrap()->bootstrap();
    }

    /**
     * 错误异常处理启动器
     *
     * @return void
     * @Author 王衍生 50634235@qq.com
     */
    static public function exceptionBootstrap()
    {
        if (!isset(self::$__exception_instance)) {
            self::$__exception_instance = kernel::single('base_handleExceptions');
        }
        return self::$__exception_instance;
    }

    /**
     * 对象单例
     *
     * @param [type] $class_name
     * @param [type] $arg
     * @return void
     * @Author 王衍生 50634235@qq.com
     */
    static public function single($class_name, $arg = null)
    {
        if (is_object($arg)) {
            $key = get_class($arg);
            if ($key === 'app') {
                $key .= '.' . $arg->app_id;
            }
            $key = '__class__' . $key;
        } else {
            $key = md5('__key__' . serialize($arg));
        }
        if (!isset(self::$__singleton_instance[$class_name][$key])) {
            self::$__singleton_instance[$class_name][$key] = new $class_name($arg);
        }
        return self::$__singleton_instance[$class_name][$key];
    }

    static public function boot()
    {
        // $pathinfo = request::getPathInfo();

        // $part = $pathinfo;

        // if ($p = strpos($pathinfo, '/', 2)) {
        //     $part = substr($pathinfo, 0, $p);
        // }

        route::run(request::instance());
    }

    /**
     * 取得app静态资源路由
     *
     * @param boolean $local_flag
     * @return void
     * @Author 王衍生 50634235@qq.com
     */
    static public function get_app_statics_host_url($local_flag = false)
    {
        return kernel::get_resource_host_url($local_flag) . '/app';
    }

    /**
     * 取得资源路由
     *
     * @param boolean $local_flag
     * @return void
     * @Author 王衍生 50634235@qq.com
     */
    static public function get_resource_host_url($local_flag = false)
    {
        return static::get_host_mirror($local_flag) ? : kernel::base_url(1);
    }

    /**
     * host 映射
     * 随机取得其中一个
     *
     * @return void
     * @Author 王衍生 50634235@qq.com
     */
    static public function get_host_mirror()
    {
        $host_mirrors = (array)config::get('storager.host_mirrors', []);
        if (!empty($host_mirrors)) {
            if (!isset(self::$__host_mirrors)) {
                self::$__host_mirrors = $host_mirrors;
                self::$__host_mirrors_count = count($host_mirrors) - 1;
            }
            return self::$__host_mirrors[rand(0, self::$__host_mirrors_count)];
        }
        return false;
    }

    static function base_url($full = false)
    {
        $c = $full ? 'true' : 'false';

        if (!isset(self::$__base_url[$c])) {
            // 使用常量定义
            if (defined('BASE_URL')) {
                if ($full) {
                    self::$__base_url[$c] = constant('BASE_URL');
                } else {
                    $url = parse_url(constant('BASE_URL'));
                    if (isset($url['path'])) {
                        self::$__base_url[$c] = $url['path'];
                    } else {
                        self::$__base_url[$c] = '';
                    }
                }
            } else {
                if(!isset(self::$base_url)){
                    // 目前的方式是保持request的纯洁性. 在base_url中做特殊处理.
                    self::$base_url = static::removeIndex(request::getBaseUrl());
                }

                if(self::$base_url == '/'){
                    self::$base_url = '';
                }

                if($full){
                    self::$__base_url[$c] = static::removeIndex(request::root());
                }else{
                    self::$__base_url[$c] = self::$base_url;
                }
            }
        }

        return self::$__base_url[$c];
    }

    /**
     * 去掉index.php
     *
     * @param [type] $root
     * @return void
     * @Author 王衍生 50634235@qq.com
     */
    static function removeIndex($root)
    {
        $i = 'index.php';
        return str_contains($root, $i) ? str_replace('/' . $i, '', $root) : $root;
    }

    static function get_lang()
    {
        return self::$__language ? : ((defined('LANG') && constant('LANG')) ? LANG : 'zh_CN');
    }

    static function set_lang($language)
    {
        self::$__language = trim($language);
    }

    static function is_online()
    {
        if(self::$__online===null){
            self::$__online = file_exists(ROOT_DIR.'/config/install.lock.php' );
        }
        return self::$__online;
    }
}